/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.ActivityHistory;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import model.LogFile;
import pojo.Activities;

/**
 *
 * @author ahmed
 */

//this class for all operations related to history data
public class ActivityHistoryCrud
{
    private   Connection conn=null;
    private PreparedStatement stmt=null;
    public ActivityHistoryCrud(Connection connection)
    {
            conn = connection;
    }

    // Add New Activity to the History
    public boolean add(ActivityHistory nextLog)
    {
         boolean check=false;
         try 
         {
             stmt = conn.prepareStatement("insert into activity_history (a_id,activity_time,status,admin_name,activity_name,provider_name,live_server) values (?,now(),?,?,?,?,?)");
             stmt.setInt(1,nextLog.getActivities().getAId());
             stmt.setString(2,nextLog.getStatus());
             stmt.setString(3,nextLog.getAdminName());
             stmt.setString(4,nextLog.getActivityName());
             stmt.setString(5,nextLog.getProviderName());
             stmt.setString(6,nextLog.getLiveServer());
             int i=stmt.executeUpdate();
             if(i == 1){
                 check = true;
                 LogFile.log(" System "," successfully logged operation with Name : ["+nextLog.getActivityName()+"] in database");
             }else{
                 check = false;
                 LogFile.log(" System "," failed to log operation with name : ["+nextLog.getActivityName()+"] in database");
             }
         }
        catch (Exception e) 
        {
            e.printStackTrace();
            LogFile.log(" System "," encountered the following exception while trying to add operation with name ["+nextLog.getActivityName()+"]in database, the problem is: ["+e+"]");
        }
        return check;
     }
     
   public boolean update(ActivityHistory nextLog ,int id)
   {
    	try {
             stmt = conn.prepareStatement("update activity_history set a_id=?,activity_time=now(),status=? where ah_id=? ");
             stmt.setInt(1,nextLog.getActivities().getAId());
             //stmt.setDate(2,new Date( nextLog.getActivityTime().getTime()));
             stmt.setString(2,nextLog.getStatus());
             stmt.setInt(3, id);

            return stmt.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteById(int id)
   {

    	try {
             stmt = conn.prepareStatement("delete from activity_history where ah_id=?");
             stmt.setInt(1,id);
             return stmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;

    }
    
    public Vector<ActivityHistory> getByProviderName(String activityName)
    {
        Vector<ActivityHistory> logs=new Vector<ActivityHistory>();
        try {
            
              stmt = conn.prepareStatement("select * from activity_history where provider_name = ?");
              stmt.setString(1, activityName );
              ResultSet result=stmt.executeQuery();
              //System.out.println("Result Set is/////////////////////////// = " +result.last());
              while(result.next())
              {
                  ActivityHistory l=new ActivityHistory();
                  
                  Activities ca=new ActivitiesDao(conn).getById( result.getInt(2)) ;
                  l.setAhId(result.getInt(1));
                  l.setActivities(ca);
                  l.setActivityName(ca.getName());
                  l.setActivityTime(result.getString(3));
                  l.setStatus(result.getString(4));
                  l.setAdminName(result.getString(5));
                  l.setProviderName(result.getString(7));
                  l.setLiveServer(result.getString(8));
              logs.add(l);
              }
              
        } catch (SQLException ex) {
            Logger.getLogger(ActivityHistoryCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        return logs;
    }

//Get History Records By Activity Time(Only History for Providers)
    public Vector<ActivityHistory> getByActivityTime(Date date,String operator)
    {
        try 
        {
            Vector<ActivityHistory> logs=new Vector<ActivityHistory>();
            stmt = conn.prepareStatement("select * from activity_history where activity_time "+operator+" ? and provider_name is not null");
            stmt.setDate(1, date);
            ResultSet result=stmt.executeQuery();
            while(result.next())
            {
                 ActivityHistory l=new ActivityHistory();
                 Activities ca=new ActivitiesDao(conn).getById( result.getInt(2)) ;
                 l.setAhId(result.getInt(1));
                 l.setActivities(ca);
                 l.setActivityName(ca.getName());
                 l.setActivityTime(result.getString(3));
                 l.setStatus(result.getString(4));
                 l.setAdminName(result.getString(5));
                 l.setProviderName(result.getString(7));
                 l.setLiveServer(result.getString(8));
              logs.add(l);
            }
            return logs;
        } catch (SQLException ex) {
            Logger.getLogger(ActivityHistoryCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    
    }
////////////////////////////////////////////Get Activity By Name////////////////////////////////////////////////
    public Activities getByName(String activityName) 
    {
        try 
        {
            stmt = conn.prepareStatement("select * from activities where name = ?");
            stmt.setString(1, activityName);
            ResultSet result=stmt.executeQuery();
            Activities act=new Activities();
            while(result.next())
            {
                 act.setAId(result.getInt(1));
                 act.setName(result.getString(3));
                 act.setDescription(result.getString(2));
            }
            return act;
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(ActivityHistoryCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
//Get History By Live Server Name
    public Vector<ActivityHistory> getByLiveServerName(String activityName) 
    {
         Vector<ActivityHistory> logs=new Vector<ActivityHistory>();
         try 
         {   
              stmt = conn.prepareStatement("select * from activity_history where live_server = ?");
              stmt.setString(1, activityName );
              ResultSet result=stmt.executeQuery();
              while(result.next())
              {
                  ActivityHistory l=new ActivityHistory();
                  Activities ca=new ActivitiesDao(conn).getById( result.getInt(2)) ;
                  l.setAhId(result.getInt(1));
                  l.setActivities(ca);
                  l.setActivityName(ca.getName());
                  l.setActivityTime(result.getString(3));
                  l.setStatus(result.getString(4));
                  l.setAdminName(result.getString(5));
                  l.setProviderName(result.getString(7));
                  l.setLiveServer(result.getString(8));
              logs.add(l);
              }
              
        } catch (SQLException ex) {
            Logger.getLogger(ActivityHistoryCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        return logs;
    }
//Get Live Server History By Date
    public Vector<ActivityHistory> getLiveServerHistoryByTime(Date date,String operator)
    {
        try 
        {
            Vector<ActivityHistory> logs=new Vector<ActivityHistory>();
            stmt = conn.prepareStatement("select * from activity_history where activity_time "+operator+" ? and live_server is not null");
            stmt.setDate(1, date);
            ResultSet result=stmt.executeQuery();
            while(result.next())
            {
                 ActivityHistory l=new ActivityHistory();
                 Activities ca=new ActivitiesDao(conn).getById( result.getInt(2)) ;
                 l.setAhId(result.getInt(1));
                 l.setActivities(ca);
                 l.setActivityName(ca.getName());
                 l.setActivityTime(result.getString(3));
                 l.setStatus(result.getString(4));
                 l.setAdminName(result.getString(5));
                 l.setProviderName(result.getString(7));
                 l.setLiveServer(result.getString(8));
              logs.add(l);
            }
            return logs;
        } catch (SQLException ex) {
            Logger.getLogger(ActivityHistoryCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
